package cn.com.majiang.controller;

import cn.com.majiang.domain.Noticetable;
import cn.com.majiang.enums.ResultCode;
import cn.com.majiang.param.NoticetableParam;
import cn.com.majiang.result.BaseResult;
import cn.com.majiang.service.INoticetableService;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 公告管理
 * Created by jiangyanxin on 2017/5/10.
 */

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/noticetable")
public class NoticeController {
    Logger logger = LoggerFactory.getLogger(NoticeController.class);

    @Autowired
    private INoticetableService noticetableService;

    /**
     * 插入公告
     *
     * @param str
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/createNoticetable", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public BaseResult createNoticetable(String str, HttpServletRequest request) throws Exception {
        logger.info("入参:" + str);
        long curr_time = System.currentTimeMillis();
        if (StringUtils.isBlank(str)) {
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        Noticetable noticetable = JSON.parseObject(str, Noticetable.class);
        if (StringUtils.isBlank(noticetable.getTitle()) || StringUtils.isBlank(noticetable.getContent())
                || noticetable.getType() == null) {
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        HttpSession session = request.getSession();
        noticetable.setCreateUser(session.getAttribute("id") + "");
        Boolean result = noticetableService.addNoticetable(noticetable);
        logger.info("插入公告耗时" + (System.currentTimeMillis() - curr_time));
        return BaseResult.build(ResultCode.REQUEST_SUCCESS.getCode(), ResultCode.REQUEST_SUCCESS.getMessage(), result);

    }

    /**
     * 搜索公告列表
     *
     * @throws Exception
     */
    @RequestMapping(value = "/searchNoticetableList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public BaseResult searchNoticetableList(String str, HttpServletRequest request) throws Exception {
        long curr_time = System.currentTimeMillis();
        logger.info("搜索公告列表入参:{}", str);
        if (StringUtils.isBlank(str)) {
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        NoticetableParam args = JSON.parseObject(str, NoticetableParam.class);
        Map<String, Object> result = noticetableService.searchNoticetableList(args);
        logger.info("搜索公告列表耗时：" + (System.currentTimeMillis() - curr_time));
        return BaseResult.build(ResultCode.REQUEST_SUCCESS.getCode(), ResultCode.REQUEST_SUCCESS.getMessage(), result);
    }


    /**
     * 编辑公告
     *
     * @param str
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/editNoticetable", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public BaseResult editNoticetable(String str, HttpServletRequest request) throws Exception {
        logger.info("编辑公告入参:" + str);
        long curr_time = System.currentTimeMillis();
        try {
            if (StringUtils.isBlank(str)) {
                return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
            }
            Noticetable noticetable = JSON.parseObject(str, Noticetable.class);
            if (noticetable.getId() == null) {
                return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
            }
            noticetable.setUpdateUser(request.getSession().getAttribute("id") + "");
            Boolean result = noticetableService.editNoticetable(noticetable);
            logger.info("编辑公告耗时" + (System.currentTimeMillis() - curr_time));
            return BaseResult.build(ResultCode.REQUEST_SUCCESS.getCode(), ResultCode.REQUEST_SUCCESS.getMessage(), result);
        } catch (Throwable t) {
            logger.error("运行时异常->NoticeController->editNoticetable", "入参:str:" + str + ",=========>>>>>", t.getMessage());
            return BaseResult.build(ResultCode.REQUEST_FAIL.getCode(), ResultCode.REQUEST_FAIL.getMessage(), "NoticeController->editNoticetable");
        }

    }


    /**
     * 查看公告
     *
     * @param str
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getNoticetable", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public BaseResult getNoticetable(String str, HttpServletRequest request) throws Exception {
        logger.info("查看公告入参:" + str);
        long curr_time = System.currentTimeMillis();
        if (StringUtils.isBlank(str)) {
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        Noticetable noticetable = JSON.parseObject(str, Noticetable.class);
        if (noticetable.getId() == null) {
            return BaseResult.build(ResultCode.NULL_REQUESTPARAM);
        }
        Map<String, String> result = noticetableService.getNoticetableById(noticetable);
        if (result != null) {
            logger.info("获取公告耗时" + (System.currentTimeMillis() - curr_time));
            return BaseResult.build(ResultCode.REQUEST_SUCCESS.getCode(), ResultCode.REQUEST_SUCCESS.getMessage(), result);
        } else {
            return BaseResult.build(ResultCode.REQUEST_FAIL);
        }

    }

}
